package net.angrynut.web.configuration;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
@MapperScan(basePackages = "net.angrynut.web.dao.mapper")
public class MybatisConfiguration {
	
	private static final String MAPPERS_PATH_ROOT = "classpath:mapper/*.xml";

	@Autowired
	private DataSource ds;

	@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(ds);
		Resource[] mappers = new PathMatchingResourcePatternResolver().getResources(MAPPERS_PATH_ROOT);
		factoryBean.setMapperLocations(mappers);
		return factoryBean.getObject();
	}

}
